#include "clock-source.h"

ClockSource::ClockSource ()
{
  _observers = 0;
}

void ClockSource::attach (ClockObserver& observer)
{
  observer._next = _observers;
  _observers = &observer;
}

void ClockSource::detach (ClockObserver& observer)
{
  if (_observers == 0)
    return;

  ClockObserver* cur = _observers;
  ClockObserver* prev = 0;

  while (cur != 0 && cur != &observer) {
    prev = cur;
    cur = cur->_next;
  }

  if (cur == 0)
    return;

  if (prev == 0)
    _observers = cur->_next;
  else
    prev->_next = cur->_next;
}

void ClockSource::tick ()
{
  ClockObserver* cur = 0;

  for (cur = _observers; cur != 0; cur = cur->_next)
    cur->clock_tick ();
}
